package android.activity;

import java.util.List;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.app.ListActivity;

import android.adapters.BankAdapter;
import android.content.Intent;
import android.loaders.BankLoader;
import android.loaders.ILoader;
import android.model.BankItem;


public class Bank extends ListActivity {
	
	/*
	 * kreiranje na objekti za rabota so loader i adapter 
	 * zaradi zacuvuvanje vo lista i citanje od xml
	 */
	ILoader<BankItem> bankLoader;
	BankAdapter bankAdapter;
	

	/*
	 * vrsi ispolnuvanje na listata so stavki
	 */
	private final void populateList() {
		try {
			List<BankItem> list = bankLoader.load();
			if (bankAdapter == null) {
				bankAdapter = new BankAdapter(this, list);
				setListAdapter(bankAdapter);
			} else {
				bankAdapter.clear();
				for (BankItem listText : list) {
					bankAdapter.add(listText);
				}
			}
			bankAdapter.notifyDataSetChanged();
		} catch (Exception e) {
		}
	}
	
	

	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 * kreiranje na activity
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		bankLoader = new BankLoader(this);
		
		
   
	}
	


	@Override
	protected void onPause() {
		super.onPause();
		populateList();
	}

	@Override
	protected void onResume() {
		super.onResume();
		populateList();
	
	}
	
	
	/*
	 * povikuvanje na nov intent
	 */
	public void showBankDialogue()
	{
		Intent bankDialogueIntent=new Intent(this,android.activity.BankDialogue.class);
		startActivity(bankDialogueIntent);
	}

	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 * kreiranje na opcionalno meni
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		super.onCreateOptionsMenu(menu);
    	//sekoja stavka koja sakame da ja ima vo menito se vnesuva
    	menu.add(0, 0, 0, "Back");
    	menu.add(0, 1, 1, "Dialogues");
    	return true;	
	}
	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 * akcii koi treba da se izvrsuvaat pri klik na nekoja stavka od menito
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch(item.getItemId())
    	{
    	case 0:
    		finish();
    		return true;
    	case 1:
    		finish();
    		showBankDialogue();
    		return true;	
    	}
    	return true;
	}



}
